package com.ecable.action;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.org.apache.commons.beanutils.BeanUtils;
import com.ecable.bean.VillageTO;
import com.ecable.delegate.VillageMgrDelegate;
import com.ecable.exception.ConnectionException;
import com.ecable.util.UtilConstants;

public class UpdateVillageAction extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8739119206017332679L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String path = "";
		RequestDispatcher rd = null;

		boolean flag = false;

		VillageTO vto = new VillageTO();

		Map map = request.getParameterMap();
		try {
			BeanUtils.populate(vto, map);
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InvocationTargetException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();

		}

		try {

			try {

				flag = new VillageMgrDelegate().updateVillage(vto);

			} catch (ConnectionException e) {
				throw new ServletException("ConnectionTO Failed");

			}

			if (flag) {

				request.setAttribute("status", UtilConstants._UPDATE_VILLAGE);

				path = UtilConstants._DISPLAY_VILLAGE_ACTION;

			} else {

				request.setAttribute("status",
						UtilConstants._UPDATE_VILLAG_FAILED);

				path = UtilConstants._DISPLAY_VILLAGE_ACTION;

			}

		} catch (Exception e) {
			e.printStackTrace();

			request.setAttribute("status", UtilConstants._INVALIED_ENTRY);

			path = UtilConstants._DISPLAY_VILLAGE_ACTION;

		}

		rd = request.getRequestDispatcher(path);

		rd.forward(request, response);

	}

}
